<template>
  <div class="cart-item">
    <img :src="item.thumb" :alt="item.name" class="item-image">
    <div class="item-details">
      <h3>{{ item.name }}</h3>
      <p>¥{{ item.price }}</p>
      <div class="quantity">
        <button @click="handleUpdateCount(item.count - 1)">-</button>
        <span>{{ item.count }}</span>
        <button @click="handleUpdateCount(item.count + 1)">+</button>
      </div>
      <button @click="cartStore.removeFromCart(item.id)">删除</button>
    </div>
  </div>
</template>

<script setup>
import { useCartStore } from '../stores/cart'

const props = defineProps({
  item: {
    type: Object,
    required: true
  }
})

const cartStore = useCartStore()

const handleUpdateCount = async (newCount) => {
  if (newCount > 0) {
    await cartStore.updateItemCount(props.item.id, newCount)
  }
}
</script>

<style lang="less" scoped>
.cart-item {
  display: flex;
  padding: 10px;
  align-items: center;
  
  + .cart-item {
    border-top: 1px solid #f8f8f8;
  }

  .item-image {
    width: 100px;
    height: 80px;
    object-fit: cover;
    margin-right: 15px;
  }

  .item-details {
    flex: 1;
    
    h3 {
      margin: 0 0 8px;
      font-size: 16px;
    }

    p {
      color: #f00;
      font-weight: bold;
      margin: 8px 0;
    }

    .quantity {
      display: flex;
      align-items: center;
      gap: 8px;
      margin: 8px 0;

      button {
        padding: 4px 8px;
      }

      span {
        min-width: 30px;
        text-align: center;
      }
    }
  }
}
</style>
